package com.yangwei.examination.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.teacher.pojo.*;
import com.yangwei.examination.teacher.service.PaperQuestionAnswerService;
import com.yangwei.examination.teacher.service.PaperQuestionChoiceService;
import com.yangwei.examination.teacher.service.PaperQuestionService;
import com.yangwei.examination.teacher.service.PaperService;
import com.yangwei.examination.user.pojo.Student;
import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

@Api( tags = "学生获取考试信息",description = "学生&试题信息")
@RestController
@RequestMapping("/stu/Paper")
public class PaperStuController {

    @Resource
    private PaperService paperService;

    @Resource
    private PaperQuestionService paperQuestionService;

    @Resource
    private PaperQuestionChoiceService paperQuestionChoiceService;

    @Resource
    private PaperQuestionAnswerService paperQuestionAnswerService;

    //开始考试
    @ApiOperation(value = "学生开始考试 按试卷id查试题信息(试题&题型)")
    @GetMapping("/findById")
    public ResultVo findById(@ApiParam(name = "id", value = "paperId唯一") @RequestParam Integer id) {

        Paper paper = paperService.getById(id);

        List<PaperQuestion> paperQuestionList = paperQuestionService.list(
                Wrappers.lambdaQuery(PaperQuestion.class)
                        .eq(PaperQuestion::getPaperId, paper.getId())
                        .orderByDesc(PaperQuestion::getQuestionOrder)
        );

        for (PaperQuestion paperQuestion : paperQuestionList) {
            //注入Choice
            List<PaperQuestionChoice> paperQuestionChoiceList = paperQuestionChoiceService.list(
                    Wrappers.lambdaQuery(PaperQuestionChoice.class)
                            .eq(PaperQuestionChoice::getQuestionId, paperQuestion.getId())
                            .orderByAsc(PaperQuestionChoice::getChoiceOrder)
            );
            paperQuestion.setPaperQuestionChoiceList(paperQuestionChoiceList);
            //注入Answer
            List<PaperQuestionAnswer> paperQuestionAnswerList = paperQuestionAnswerService.list(
                    Wrappers.lambdaQuery(PaperQuestionAnswer.class)
                            .eq(PaperQuestionAnswer::getQuestionId, paperQuestion.getId())
                            .orderByAsc(PaperQuestionAnswer::getAnswerOrder)
            );
            paperQuestion.setPaperQuestionAnswerList(paperQuestionAnswerList);
        }
        paper.setPaperQuestionList(paperQuestionList);

        PaperInfo paperInfo = new PaperInfo();
        for (int i = 1; i <= 4; i++) {
            QueryWrapper<PaperQuestion> paperQuestionQueryCount = new QueryWrapper<>();
            paperQuestionQueryCount.lambda().eq(PaperQuestion::getQuestionType, i).eq(PaperQuestion::getPaperId, paper.getId());
            if (i == 1)
                paperInfo.setSingleNum(paperQuestionService.count(paperQuestionQueryCount));
            if (i == 2)
                paperInfo.setMultipleNum(paperQuestionService.count(paperQuestionQueryCount));
            if (i == 3)
                paperInfo.setFillInNum(paperQuestionService.count(paperQuestionQueryCount));
            if (i == 4)
                paperInfo.setTextNum(paperQuestionService.count(paperQuestionQueryCount));
        }
        paper.setPaperInfo(paperInfo);

        return ResultUtils.success("查询成功", paper);
    }

}